#include <SPI.h>
#include <Ethernet.h>
#include <Twitter.h>

String m_sTweet = "";

void setup() {
  Serial.begin(9600);
}

void loop() {

    if ( Serial.available() > 0 ) {	//if data is received
        int byData = Serial.read();

        m_sTweet += (char)byData;

        if( byData == 0x03 ) { //EOT
            sendTweet( m_sTweet );
        }
    }
}

void sendTweet( String sTweet ) {
    Twitter twitter( "API KEY HERE" );
    byte byMAC[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
    byte byIP[]  = { 192, 168, 1, 116 };

    delay( 1000 );
    Ethernet.begin( byMAC, byIP );
    Serial.println( "Connecting to Twitter Service..." );

    int nTweetBufferfLenght = sTweet.length( );
    char tweetBuffer[ nTweetBufferfLenght ];
    sTweet.toCharArray( tweetBuffer, nTweetBufferfLenght );

    Serial.print( "Message: " );
    Serial.println( tweetBuffer );

    if ( twitter.post( tweetBuffer ) ) {
        twitter.wait( &Serial );
        //String strStatus = String( twitter.wait( &Serial ) );
        //Serial.println( buffer );
    }   else {
        Serial.println("Connection Failed.");
    }
    m_sTweet = "";
}




